/*
 * Copyright 2016 SoChishun.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ucomponse;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import util.ICallback;

/**
 *
 * @author SoChishun
 */
public class SuePagination extends javax.swing.JPanel {

    int pageId = 0;
    int recordCount = 0;
    int pageSize = 0;
    int pageCount = 0;

    public void setPagination(int recordCount, int pageSize) {
        this.pageSize = pageSize;
        this.recordCount=recordCount;
        this.pageCount = (int) Math.ceil(recordCount / pageSize);
        cbb_pages.removeAllItems();
        if (pageCount > 0) {
            for (int i = 1; i <= pageCount; i++) {
                cbb_pages.addItem(String.valueOf(i));
            }
        }
        setMessage(1);
    }

    public void setPagingAction(SuePaginationCallback callback) {
        btn_first.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                pageId=1;
                callback.setPage(pageId);
                setMessage(pageId);
            }
        });
        btn_prev.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                pageId = pageId - 1;
                if (pageId < 1) {
                    pageId = 1;
                }
                callback.setPage(pageId);
                setMessage(pageId);
            }
        });
        btn_next.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                pageId = pageId + 1;
                if (pageId > pageCount) {
                    pageId = pageCount;
                }
                callback.setPage(pageId);
                setMessage(pageId);
            }
        });
        btn_last.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                pageId=pageCount;
                callback.setPage(pageId);
                setMessage(pageId);
            }
        });
        cbb_pages.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object item = cbb_pages.getSelectedItem();
                if (null != item) {
                    pageId = Integer.parseInt(item.toString());
                    callback.setPage(pageId);
                    setMessage(pageId);
                }
            }
        });
    }

    public void setPage(int page) {
        pageId = page;
        setMessage(page);
    }

    void setMessage(int page) {
        lbl_message.setText(String.format("页次: %d/%d, 数量: %d/%d", page, pageCount, pageSize, recordCount));
    }

    /**
     * Creates new form SuePagination
     */
    public SuePagination() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbl_message = new javax.swing.JLabel();
        btn_prev = new javax.swing.JButton();
        btn_first = new javax.swing.JButton();
        btn_next = new javax.swing.JButton();
        btn_last = new javax.swing.JButton();
        cbb_pages = new javax.swing.JComboBox<>();

        lbl_message.setText("页次: 0/0, 数量: 0/0");

        btn_prev.setText("上一页");

        btn_first.setText("首页");

        btn_next.setText("下一页");

        btn_last.setText("末页");

        cbb_pages.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(btn_first)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btn_prev)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btn_next)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btn_last)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbb_pages, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lbl_message)
                .addContainerGap(107, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(lbl_message)
                .addComponent(btn_first)
                .addComponent(btn_prev)
                .addComponent(btn_next)
                .addComponent(btn_last)
                .addComponent(cbb_pages, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_first;
    private javax.swing.JButton btn_last;
    private javax.swing.JButton btn_next;
    private javax.swing.JButton btn_prev;
    private javax.swing.JComboBox<String> cbb_pages;
    private javax.swing.JLabel lbl_message;
    // End of variables declaration//GEN-END:variables
}
